/*
 * Copyright (c) 2012-2013, Lucky Air Company Ltd,.
 *
 * All rights reserved.
 */
package com.yuanxin.app.ftpfilter.service;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

@Service
public class FtpfilterJob {
	public static String ftpPath = "D:/fildir";
	
	public static String ftpTargetPath = "D:/u-fly-ftp";
	public void filterinfo() throws Exception {
		
		Date d1 = new Date();
		
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		String month = (c.get(Calendar.MONTH) + 1)<10?"0"+(c.get(Calendar.MONTH) + 1):(c.get(Calendar.MONTH) + 1)+"";
		String day = (c.get(Calendar.DAY_OF_MONTH))<10?"0"+(c.get(Calendar.DAY_OF_MONTH)):(c.get(Calendar.DAY_OF_MONTH))+"";
		
		//PNRD分类
		String filename = "/pnrd/8L_PNRD_" + c.get(Calendar.YEAR) + month + day +  ".txt";
		String childpath = "/pnrd";
		//过滤
		String filter = "46-48;55-61;63-74;253-286;288-296;303-401";
		
		jobutil(filename , childpath , filter);
//		
		
		//CPD分类
		filename = "/cki/8L_CPD_" + c.get(Calendar.YEAR) + month + day +  ".txt";
		childpath = "/cki";
		//过滤
		filter = "158-162;166-174;189-200;222-241;247-319;362-364;371-398";
		jobutil(filename , childpath , filter);
		
		//TKTCRS分类
		filename = "/tkt/8L_TKTICS_" + c.get(Calendar.YEAR) + month + day +  ".txt";
		childpath = "/tkt";
		//过滤
		Map filterMap = new HashMap();
		filterMap.put("01", "3-11;26-59;73-92;105-108;165-166;182-198;220-333;340-401");
		filterMap.put("02", "3-11;26-43;57-167;209-215;249-255;289-295;329-401");
		filterMap.put("03", "3-11;26-43;67-82;166-215;294-401");
		filterMap.put("04", "3-11;26-43;84-190;372-401");
		filterMap.put("05", "3-11;26-43;44-46;83-93;123-132;133-135;172-182;212-221;222-224;261-271;301-310;311-313;350-360;390-401");
		filterMap.put("07", "3-11;26-43;64-166;187-289;389-401");
		filterMap.put("08", "3-11;26-43;132-133;220-221;308-309;396-398");
		filterMap.put("09", "3-11;26-59;78-110;129-161;180-212;231-263;282-314;333-401");
		filterMap.put("98", "3-11;26-61;121-157;177-381");
		filterMap.put("11", "3-229");
		
		jobutilForFilterMap(filename , childpath , filterMap);
		
		//TKTBSP分类
		filename = "/tkt/8L_TKTBSP_" + c.get(Calendar.YEAR) + month + day +  ".txt";
		childpath = "/tkt";
		//过滤
		filterMap = new HashMap();
		filterMap.put("01", "3-11;26-59;73-92;105-108;165-166;182-198;220-333;340-401");
		filterMap.put("02", "3-11;26-43;57-167;209-215;249-255;289-295;329-401");
		filterMap.put("03", "3-11;26-43;67-82;166-215;294-401");
		filterMap.put("04", "3-11;26-43;84-190;372-401");
		filterMap.put("05", "3-11;26-43;44-46;83-93;123-132;133-135;172-182;212-221;222-224;261-271;301-310;311-313;350-360;390-401");
		filterMap.put("07", "3-11;26-43;64-166;187-289;389-401");
		filterMap.put("08", "3-11;26-43;132-133;220-221;308-309;396-398");
		filterMap.put("09", "3-11;26-59;78-110;129-161;180-212;231-263;282-314;333-401");
		filterMap.put("98", "3-11;26-61;121-157;177-381");
		filterMap.put("11", "3-229");
		
		jobutilForFilterMap(filename , childpath , filterMap);
		
		//TKTCRS分类
		filename = "/tkt/8L_TKTCRS_" + c.get(Calendar.YEAR) + month + day +  ".txt";
		childpath = "/tkt";
		//过滤
		filterMap = new HashMap();
		filterMap.put("01", "3-11;26-59;73-92;105-108;165-166;182-198;220-333;340-401");
		filterMap.put("02", "3-11;26-43;57-167;209-215;249-255;289-295;329-401");
		filterMap.put("03", "3-11;26-43;67-82;166-215;294-401");
		filterMap.put("04", "3-11;26-43;84-190;372-401");
		filterMap.put("05", "3-11;26-43;44-46;83-93;123-132;133-135;172-182;212-221;222-224;261-271;301-310;311-313;350-360;390-401");
		filterMap.put("07", "3-11;26-43;64-166;187-289;389-401");
		filterMap.put("08", "3-11;26-43;132-133;220-221;308-309;396-398");
		filterMap.put("09", "3-11;26-59;78-110;129-161;180-212;231-263;282-314;333-401");
		filterMap.put("98", "3-11;26-61;121-157;177-381");
		filterMap.put("11", "3-229");
		
		jobutilForFilterMap(filename , childpath , filterMap);
		
		Date d2 = new Date();
		System.out.println("执行日期：" + d2.toString() + "--耗时：" + (d2.getTime()-d1.getTime()));
	}
	
	public static void jobutil(String filename, String childpath ,String filter){
		//解压
		UnRarFile unrar = new UnRarFile();  
		String unfilePath = ftpPath + filename + ".Z";
		String yuanfilePath = ftpPath + filename;
		String targetPath = ftpTargetPath + filename;
		
		unrar.unRarFileCurDir(new File(unfilePath));
		
		unrar.readTxtFile(yuanfilePath , targetPath , filter);
		//压缩
		unrar.zip(targetPath);
		File f = new File(targetPath);
		f.delete();
		//删除解压后源文件
		File yuan = new File(yuanfilePath);
		yuan.delete();
	}
	
	public static void jobutilForFilterMap(String filename, String childpath ,Map filter){
		//解压
		UnRarFile unrar = new UnRarFile();  
		String unfilePath = ftpPath + filename + ".Z";
		String yuanfilePath = ftpPath + filename;
		String targetPath = ftpTargetPath + filename;
		
		unrar.unRarFileCurDir(new File(unfilePath));
		
		unrar.readTxtFileForFilterMap(yuanfilePath , targetPath , filter);
		//压缩
		unrar.zip(targetPath);
		File f = new File(targetPath);
		f.delete();
		//删除解压后源文件
		File yuan = new File(yuanfilePath);
		yuan.delete();
	}
	
	public static void main(String[] args) {  
		FtpfilterJob x = new FtpfilterJob();
		try {
			x.filterinfo();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
